﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

using Avatar.MembershipService;
using Avatar.BLL;

public partial class serverVariables : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadsystemInfo();
        }
    }

    /// <summary>
    /// 检测系统信息
    /// </summary>
    protected void LoadsystemInfo()
    {
        // 取得服务器相关信息
        servername.Text = Server.MachineName;
        serverip.Text = Request.ServerVariables["LOCAL_ADDR"];
        server_name.Text = Request.ServerVariables["HTTP_HOST"];

        int build, major, minor, revision;
        build = Environment.Version.Build;
        major = Environment.Version.Major;
        minor = Environment.Version.Minor;
        revision = Environment.Version.Revision;
        servernet.Text = ".NET CLR  " + major + "." + minor + "." + build + "." + revision;
        serverms.Text = Environment.OSVersion.ToString();

        serversoft.Text = Request.ServerVariables["SERVER_SOFTWARE"];
        serverport.Text = Request.ServerVariables["SERVER_PORT"];
        serverout.Text = Server.ScriptTimeout.ToString();
        // 语言应该是浏览者信息, 1.0 final 修改
        cl.Text = Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"];
        servertime.Text = DateTime.Now.ToString();
        serverppath.Text = Request.ServerVariables["APPL_PHYSICAL_PATH"];
        servernpath.Text = Request.ServerVariables["PATH_TRANSLATED"];
        serverhttps.Text = Request.ServerVariables["HTTPS"];

        // 取得用户浏览器信息
        HttpBrowserCapabilities bc = Request.Browser;
        ie.Text = bc.Browser.ToString();
        cookies.Text = bc.Cookies.ToString();
        frames.Text = bc.Frames.ToString();
        javaa.Text = bc.JavaApplets.ToString();
        javas.Text = bc.EcmaScriptVersion.ToString();
        ms.Text = bc.Platform.ToString();
        vbs.Text = bc.VBScript.ToString();
        vi.Text = bc.Version.ToString();

        // 取得浏览者ip地址,1.0 final 加入
        cip.Text = GetIP(); // Request.ServerVariables["REMOTE_ADDR"];
    }

    protected void Bate_5kw_Click(object sender, EventArgs e)
    {
        #region 测试5千万次的加法运算
        
        int num = 0;
        for (int i = 1; i <= 50000000; i++)
        {
            num++;
        }
        //Model_Time.EndTime = DateTime.Now;
        //TimeSpan ts = Model_Time.EndTime - Model_Time.StartTime;
        //l5000.Text = ts.TotalSeconds.ToString("f6") + "毫秒";
        #endregion
    }

    /// <summary>
    /// 获得当前页面客户端的IP
    /// </summary>
    /// <returns>当前页面客户端的IP</returns>
    public static string GetIP()
    {
        string result = String.Empty;

        result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(result))
        {
            result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }
        if (string.IsNullOrEmpty(result))
        {
            result = HttpContext.Current.Request.UserHostAddress;
        }
        if (string.IsNullOrEmpty(result) || !IsIP(result))
        {
            return "127.0.0.1";
        }
        return result;
    }

    /// <summary>
    /// 是否为ip
    /// </summary>
    /// <param name="ip"></param>
    /// <returns></returns>
    public static bool IsIP(string ip)
    {
        return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
    }
}
